﻿
using AutoMapper;
using MediatR;
using Org.BouncyCastle.Ocsp;
using ProductionManagement.Application.Command.ScrapsCommand;
using ProductionManagement.Domain.ProductionDomain;
using ProductionManagement.Infrstructre.lnterface;
using SqlSugar;

namespace ProductionManagement.Application.CommandHandler.ScrapsCommandHandler
{
    public class RuKuScrapsCommandHandler : IRequestHandler<RuKuScrapsCommand, int>
    {
        IProductionWasteRepository pwRep;
        IProduceScrapsProductsRepository pspRep;
        ISqlSugarClient db;
        IMapper mapper;

        public RuKuScrapsCommandHandler(IProductionWasteRepository pwRep, ISqlSugarClient db, IMapper mapper, IProduceScrapsProductsRepository pspRep)
        {
            this.pwRep = pwRep;
            this.db = db;
            this.mapper = mapper;
            this.pspRep = pspRep;
        }

        public async Task<int> Handle(RuKuScrapsCommand request, CancellationToken cancellationToken)
        {
            // 开始事务
            db.Ado.BeginTran();

            try
            {
                //修改废料
                var feed = (await pwRep.GetAll()).Where(w => w.PWId == request.PWId).First();
                feed.PIsStorage = 3;//已入库
                await pwRep.UpdAsync(feed);
                // 提交事务
                db.Ado.CommitTran();

                return 1;  // 返回最终的结果
            }
            catch (Exception)
            {
                // 回滚事务
                db.Ado.RollbackTran();
                throw;
            }
        }

    }
}
